<!DOCTYPE html>
<body>
<input type="date" id="test1">
<input type="date" id="test2" min="1999-01-01" max="2000-12-31">
<script>
function fieldsText(input) {
    return internals.shadowRoot(input).textContent;
}

function back(newState) {
    parent.state = newState;
    history.back();
}

function forward(newState) {
    parent.state = newState;
    history.forward();
}

function keyDown(key) {
    eventSender.keyDown(key);
}

var testInput1 = document.getElementById('test1');
var testInput2 = document.getElementById('test2');
var pageName = location.search || '?page1';

window.onload = function() { setTimeout(function() {
    switch (pageName) {
    case '?page1':
        switch (parent.state) {
        case 'direct': {
            testInput1.value = '12012-10-09';
            testInput2.focus();
            keyDown('ArrowUp'); // -> [01]/dd/yyyy
            keyDown('\t'); //      -> 01/[dd]/yyyy
            keyDown('\t'); //      -> 01/dd/[yyyy]
            keyDown('ArrowUp'); // -> 01/dd/[1999]
            var nextUri = location.toString().replace('?page1', '?page2');
            setTimeout(function() { window.location.href = nextUri; }, 0);
            break;
        }
        case 'backTo1':
            parent.testCases.push(
                {
                    'description' : 'Went back to a page. Checking a form control which had a full value',
                    'actual' : testInput1.value,
                    'expected' : '12012-10-09' },
                {
                    'description' : 'Went back to a page. Checking a form control which had a partial value',
                    'actual' : fieldsText(testInput2),
                    'expected' : '01/dd/1999'
                });
            forward('forwardTo2');
            break;
        case 'backTo1Second':
            parent.finishTest();
            break;
        default:
            parent.failed('Unexpected state(' + parent.state + ')');
            break;
        }
        break;

    case '?page2':
        switch (parent.state) {
        case 'direct':
            testInput1.value = '0001-11-17';
            testInput2.focus();
            keyDown('\t'); //        -> mm/[dd]/yyyy
            keyDown('ArrowDown'); // -> mm/[31]/yyyy
            back('backTo1');
            break;
        case 'forwardTo2':
            parent.testCases.push(
                {
                    'description' : 'Went forward to a page. Checking a form control which had a full value',
                    'actual' : testInput1.value,
                    'expected' : '0001-11-17'
                },
                {
                    'description' : 'Went forward to a page. Checking a form control which had a partial value',
                    'actual' : fieldsText(testInput2),
                    'expected' : 'mm/31/yyyy'
                });
            back('backTo1Second');
            break;
        default:
            parent.failed('Unexpected state(' + parent.state + ')');
            break;
        }
        break;

    default:
        parent.failed('Unexpected page(' + pageName + ')');
        break;
    }
}, 1); };
</script>
</body>
